import random

users = [] #玩家列表
msg = ''
gamemap = None
user = None

class User: #玩家类
	position = None
	direction =None
	
	property = {
		'饱食度': 10,
		'最大步长': 3
	}

	def __init__(self, nickname, token): #初始化玩家信息
		self.nickname = nickname
		self.token = token
		
	def update_section(self):
		if user.position[0] in range(gamemap.section1[0][0], gamemap.section1[0][1]) and user.position[1] in range(gamemap.section1[1][0], gamemap.section1[1][1]):
			gamemap.section = gamemap.section1
		elif user.position[0] in range(gamemap.section2[0][0], gamemap.section2[0][1]) and user.position[1] in range(gamemap.section2[1][0], gamemap.section2[1][1]):
			gamemap.section = gamemap.section2
		elif user.position[0] in range(gamemap.section3[0][0], gamemap.section3[0][1]) and user.position[1] in range(gamemap.section3[1][0], gamemap.section3[1][1]):
			gamemap.section = gamemap.section3
		elif user.position[0] in range(gamemap.section4[0][0], gamemap.section4[0][1]) and user.position[1] in range(gamemap.section4[1][0], gamemap.section4[1][1]):
			gamemap.section = gamemap.section4
		elif user.position[0] in range(gamemap.section5[0][0], gamemap.section5[0][1]) and user.position[1] in range(gamemap.section5[1][0], gamemap.section5[1][1]):
			gamemap.section = gamemap.section5
		elif user.position[0] in range(gamemap.section6[0][0], gamemap.section6[0][1]) and user.position[1] in range(gamemap.section6[1][0], gamemap.section6[1][1]):
			gamemap.section = gamemap.section6
		elif user.position[0] in range(gamemap.section7[0][0], gamemap.section7[0][1]) and user.position[1] in range(gamemap.section7[1][0], gamemap.section7[1][1]):
			gamemap.section = gamemap.section7
		elif user.position[0] in range(gamemap.section8[0][0], gamemap.section8[0][1]) and user.position[1] in range(gamemap.section8[1][0], gamemap.section8[1][1]):
			gamemap.section = gamemap.section8
		elif user.position[0] in range(gamemap.section9[0][0], gamemap.section9[0][1]) and user.position[1] in range(gamemap.section9[1][0], gamemap.section9[1][1]):
			gamemap.section = gamemap.section9
			
class GameMap:
	forest = {
		'name' : '森林',
		'森林' : 20,
		'矿' : 10,
		'空地' : 70,
	}
	
	mine = {
		'name' : '矿洞',
		'森林' : 10,
		'矿' : 20,
		'空地' : 70,
	}
	
	plain = {
		'name' : '平原',
		'森林' : 10,
		'矿' : 10,
		'空地' : 80,
	}

	def __init__(self, mapid):
		if mapid == '1':
			self.create_map(self.forest)
		elif mapid == '2':
			self.create_map(self.mine)
		elif mapid == '3':
			self.create_map(self.plain)
		else:
			send_msg("你输入的信息有误，请从新输入")
			select_map()
			
	def create_map(self, map_dict):
		self.map = [[0 for i in range(30)] for i in range(30)]
		source = []
		map_index = []
		forest = map_dict['森林']
		mine = map_dict['矿']
		
		for i in range(int(900 * forest / 100)):
			source.append('树')
			
		for i in range(int(900 * mine / 100)):
			source.append('矿')
			
		for i in range(len(self.map)):
			for j in range(len(self.map[i])):
				map_index.append( (i, j) )
				
		for i in range(len(source)):
			index = random.randint(0, len(map_index) - 1)
			self.map[map_index[index][0]][map_index[index][1]] = source[i]
			del map_index[index]
			
		#设置人物位置
		for i in range(len(self.map)):
			for j in range(len(self.map[i])):
				if self.map[i][j] == 0:
					global user
					self.map[i][j] = user.token
					user.position = (i, j)
					break
			break
		
	def set_section(self):
		self.section1 = [ (0, 10), (0, 10) ] #[begin, end) [x, y]
		self.section2 = [ (10, 20), (0, 10) ]
		self.section3 = [ (20, 30), (0, 10) ]
		
		self.section4 = [ (0, 10), (10, 20) ]
		self.section5 = [ (10, 20), (10, 20) ]
		self.section6 = [ (20, 30), (10, 20) ]
		
		self.section7 = [ (0, 10), (20, 30) ]
		self.section8 = [ (10, 20), (20, 30) ]
		self.selection9 = [ (20, 30), (20, 30) ]
		
		self.section =None
		#判断人物所在的区域
		user.update_section()
		
		self.print_fullmap()
		print('*' * 20 + '分割线' + '*' * 20)
		self.print_map()
		
	def print_fullmap(self):
		for i in range(len(self.map)):
			for j in self.map[i]:
				if j == 0:
					send_msg('  ', mend = '')
				else:
					send_msg(j, mend = '')
			send_msg('')
			
	def print_map(self):
		print('    ', end='')
		for i in range(10):
			print(' ' + str(i), end='')
		print()
		print('  ', end = '')
		print('＊' * 12)
		
		for i in range(self.section[1][0], self.section[1][1]):
			send_msg(str(i) + ' ＊', mend = '')
			for j in range(self.section[0][0], self.section[0][1]):
				if self.map[i][j] == 0:
					send_msg('  ', mend = '')
				else:
					send_msg(self.map[i][j], mend = '')
				
				if j == self.section[0][1] - 1:
					send_msg('＊', mend = '')
					send_msg('')
					
		print('  ', end = '')
		print('＊' * 12)
			
def get_user_nickname():
	send_msg('请输入你的昵称：')
	recive_msg(input())
	nickname = msg
	
	get_user_token(nickname)
	
def get_user_token(nickname):
	send_msg('请输入你的代表符号（一个字）:')
	recive_msg(input())
	token = msg[0]
	
	if token.isalnum() or (token >= u'\u4e00'and token <= u'\u9fff'):
		if token.isalnum():
			token = ' ' + token
			
		global user
		user = User(nickname, token)
		users.append(user)
	else:
		send_msg('你输入的代表符号有误！请重新输入')
		get_user_token(nickname)
	
def select_map():
	send_msg('请选择地图（输序号）：\n【1】森林（树：20%，矿：10%，空：70%）\n【2】矿洞（树：10%，矿：20%，空：70%）\n【3】平原（树：10%，矿：10%，空：80%）')
	recive_msg(input())
	mapid = msg
	global gamemap
	gamemap = GameMap(mapid)
	
def move(direction, length):
	pass
	
def loop_run():
	while True:
		send_msg('>>>')
		recive_msg(input())
		action = msg
		actionlist = action.split('|')
	
		if len(actionlist) == 2:
			action_type = actionlist[0]
			action_value = actionlist[1]
		
			if action_type.replace(' ', '') == '移动':
				directions = ['上', '下', '左', '右']
				direction = action_value.replace(' ', '')[0]
				length = action_value.replace(' ', '')[1:]
			
				if direction in directions and length.isdigit():
					move(direction, length)
				else:
					send_msg('指令有误！！！输入：查看|指令，来查询指令')
	
def game_start():
	get_user_nickname()
	select_map()
	gamemap.set_section()
	loop_run()
	
def send_msg(msg, mend = '\n'):
	print(msg, end = mend)
	
def recive_msg(str):
	global msg
	msg = str
	
if __name__ == '__main__':
	game_start()
	print(users)
